package com.rxh.service.impl;


import com.rxh.mapper.finance.FinanceDrawingMapper;
import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.mapper.merchant.MerchantSettingMapper;
import com.rxh.pojo.finance.FinanceDrawing;
import com.rxh.pojo.merchant.MerchantInfo;
import com.rxh.service.DrawingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DrawingServiceImpl implements DrawingService {

    @Resource
    private FinanceDrawingMapper drawingMapper;

    @Autowired
    private MerchantInfoMapper merchantInfoMapper;

    @Autowired
    MerchantSettingMapper merchantSettingMapper;

    @Override
    public Map<String, Object> save(FinanceDrawing entity) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int deleteById(String id) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int deleteByParam(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Map<String, Object> update(FinanceDrawing entity) {

        Map<String, Object> map = new HashMap<String, Object>();
        int affectCount = drawingMapper.updateByPrimaryKeySelective(entity);
        if (affectCount > 0) {

            map.put("success", 1);

        } else {

            map.put("success", 0);

        }

        return map;

    }

    @Override
    public int updateByIdSelective(FinanceDrawing entity) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public FinanceDrawing selectById(String id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<FinanceDrawing> queryList(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<FinanceDrawing> queryDrawingAmount(Map<String, Object> map) {
        return drawingMapper.queryDrawingAmount(map);
    }

    @Override
    public Map<String, Object> getDrawingInit() {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("merchants", merchantInfoMapper.selectAllIdAndName());
        return resultMap;

    }

    @Override
    public Map<String, Object> getDrawingInfo(Map<String, Object> paramMap) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> searchInfoMap = (Map<String, Object>) paramMap.get("searchInfo");

        if (null != searchInfoMap) {
            paramMap.put("refId", searchInfoMap.get("refId"));
            paramMap.put("applicant", searchInfoMap.get("applicant"));
            paramMap.put("minApplicationTime", searchInfoMap.get("minApplicationTime"));
            paramMap.put("maxApplicationTime", searchInfoMap.get("maxApplicationTime"));
            paramMap.put("transfer", searchInfoMap.get("transfer"));
            paramMap.put("status", searchInfoMap.get("status"));
            paramMap.put("statusString", searchInfoMap.get("statusString"));
            paramMap.put("minTransferTime", searchInfoMap.get("minTransferTime"));
            paramMap.put("maxTransferTime", searchInfoMap.get("maxTransferTime"));
        }
        paramMap.put("sortFieldName", paramMap.get("sortFieldName"));
        paramMap.put("sortType", paramMap.get("sortType"));
        paramMap.put("pageFirst", paramMap.get("pageFirst"));
        paramMap.put("pageSize", paramMap.get("pageSize"));
        List<FinanceDrawing> drawingList = drawingMapper.queryList(paramMap);
        int resultTotal = drawingMapper.getDrawingAllResultCount(paramMap);
        resultMap.put("resultTotal", resultTotal);
        for (FinanceDrawing drawing : drawingList) {
            Integer merId = drawing.getRefId();
            if (merId != null) {
                MerchantInfo merchantInfo = merchantInfoMapper.selectByPrimaryKey(merId);
                if (null != merchantInfo) {
                    drawing.setMerName(merchantInfo.getName());
                }
            }
        }
        resultMap.put("drawingList", drawingList);
        return resultMap;
    }

    @Override
    public Map<String, Object> getDrawingTransferUpdateInit(Integer merId) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("merchant", merchantInfoMapper.selectByPrimaryKey(merId));
        resultMap.put("merchantSetting", merchantSettingMapper.selectByPrimaryKey(merId));
        return resultMap;

    }


    public Map<String, Object> queryDrawingStatistics(FinanceDrawing drawing) {

        Map<String, Object> paramAndResultMap = new HashMap<String, Object>();
        paramAndResultMap.put("refId", drawing.getRefId());
        paramAndResultMap.put("applicant", drawing.getApplicant());
        paramAndResultMap.put("appstartDate", drawing.getMinApplicationTime());
        paramAndResultMap.put("appendDate", drawing.getMaxApplicationTime());
        paramAndResultMap.put("transfer", drawing.getTransfer());
        paramAndResultMap.put("status", drawing.getStatus());

        drawingMapper.queryDrawingStatistics(paramAndResultMap);
        Map<String, Object> resultMap = paramAndResultMap;
        int drawingCurrencyColumns = 0;
        int drawingAmountColumns = 0;
        int transferCurrencyColumns = 0;
        int transferAmountColumns = 0;
        int transferRateColumns = 0;

        String acquirerNameStr = String.valueOf(resultMap.get("acquirerNameStr"));
        if (null != acquirerNameStr && !acquirerNameStr.equals("")) {
            acquirerNameStr = acquirerNameStr.substring(0, acquirerNameStr.length() - 1);
            resultMap.put("acquirerNameStr", acquirerNameStr);
        }

        String drawingCurrencyStr = String.valueOf(resultMap.get("drawingCurrencyStr"));
        String[] drawingCurrencyArray = null;
        if (null != drawingCurrencyStr && !drawingCurrencyStr.equals("")) {
            drawingCurrencyArray = drawingCurrencyStr.split(";");
            drawingCurrencyColumns = drawingCurrencyArray.length;
        }

        String drawingAmountStr = String.valueOf(resultMap.get("drawingAmountStr"));
        String[] drawingAmountArray = null;
        if (null != drawingAmountStr && !drawingAmountStr.equals("")) {
            drawingAmountArray = drawingAmountStr.split(";");
            drawingAmountColumns = drawingAmountArray.length;
        }

        String transferCurrencyStr = String.valueOf(resultMap.get("transferCurrencyStr"));
        String[] transferCurrencyArray = null;
        if (null != transferCurrencyStr && !transferCurrencyStr.equals("")) {
            transferCurrencyArray = transferCurrencyStr.split(";");
            transferCurrencyColumns = transferCurrencyArray.length;
        }

        String transferAmountStr = String.valueOf(resultMap.get("transferAmountStr"));
        String[] transferAmountArray = null;
        if (null != transferAmountStr && !transferAmountStr.equals("")) {
            transferAmountArray = transferAmountStr.split(";");
            transferAmountColumns = transferAmountArray.length;
        }

        String transferRateStr = String.valueOf(resultMap.get("transferRateStr"));
        String[] transferRateArray = null;
        if (null != transferRateStr && !transferRateStr.equals("")) {
            transferRateArray = transferRateStr.split(";");
            transferRateColumns = transferRateArray.length;
        }

        BigDecimal transferTotalAmount = new BigDecimal(0);
        if (null != transferAmountArray && transferAmountArray.length > 0) {
            for (String transferAmount : transferAmountArray) {
                if (!transferAmount.equals("")) {
                    BigDecimal transferAmountObject = new BigDecimal(transferAmount);
                    transferTotalAmount = transferTotalAmount.add(transferAmountObject);

                }


            }
        }
        resultMap.put("drawingCurrencyColumns", drawingCurrencyColumns);
        resultMap.put("drawingAmountColumns", drawingAmountColumns);
        resultMap.put("transferCurrencyColumns", transferCurrencyColumns);
        resultMap.put("transferAmountColumns", transferAmountColumns);
        resultMap.put("transferRateColumns", transferRateColumns);
        resultMap.put("transferTotalAmount", transferTotalAmount);

        resultMap.put("drawingCurrencyArray", drawingCurrencyArray);
        resultMap.put("drawingAmountArray", drawingAmountArray);
        resultMap.put("transferCurrencyArray", transferCurrencyArray);
        resultMap.put("transferAmountArray", transferAmountArray);
        resultMap.put("transferRateArray", transferRateArray);

        return resultMap;
    }



    @Override
    public List<FinanceDrawing> getExcelInfo(Map<String, Object> map) {



        return drawingMapper.selectDrawingInfo(map);
    }


}
